import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import path from 'path'
const pathSrc = path.resolve(__dirname, 'src')

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
	const env = loadEnv(mode, process.cwd())
	const { VITE_APP_BASE_URL, VITE_APP_BASE_PRE } = env
	return {
		transpileDependencies: ['uview-plus'],
		plugins: [
			uni(),
			AutoImport({
				imports: ['vue', 'uni-app'],
				// 生成自动导入的TS声明文件
				vueTemplate: true,
				dts: path.resolve(pathSrc, 'types', 'auto-imports.d.ts')
			})
		],
		resolve: {
			// 别名
			alias: {
				'@': path.join(__dirname, './src')
			}
		},
		server: {
			proxy: {
				[VITE_APP_BASE_PRE]: {
					target: VITE_APP_BASE_URL,
					ws: true,
					changeOrigin: true,
					// 要记得加rewrite这句
					// rewrite: (path) => path.replace(/^\/apis/, ''),
					rewrite: (path) => path.replace(new RegExp('^' + VITE_APP_BASE_PRE), '')
				}
			}
		}
	}
})